from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from .models import Area
from .serializers import AreasInfosSerializer,AreasInfosSuperSerializer
from rest_framework.response import Response
# Create your views here.

#######################################省、市、县三级联动  一级视图####################################################
# class AreasProvinceView(APIView):
#
#     def get(self,request):
#         area = Area.objects.filter(parent =None).all()
#         serializer = AreasInfosSerializer(area,many=True)
#         return Response(serializer.data)

# class AreasSubsCityView(APIView):
#
#     def get(self,request,id):
#         area = Area.objects.filter(parent = id).all()
#         serializer = AreasInfosSerializer(area,many=True)
#         return Response(serializer.data)


#######################################省、市、县三级联动  二级视图####################################################
# class AreasInfosView(GenericAPIView):
#
#     def get_serializer(self,area):
#         return AreasInfosSerializer(area,many=True)
#
#     def get(self,request,pk):
#         area = Area.objects.filter(parent=id).all()
#         serializer = self.get_serializer(area)
#         return Response(serializer.data)

#######################################省、市、县三级联动  视图集####################################################
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import IsAuthenticated

class AreasInfosView(ReadOnlyModelViewSet):
    # permission_classes = [IsAuthenticated]
    pagination_class = None

    def get_queryset(self):
        if self.action =='list':
            return Area.objects.filter(parent=None).all()
        else:
            # return Area.objects.all()
            return Area.objects.all()


    def get_serializer_class(self):

        if self.action=='list':
            return AreasInfosSerializer
        else:
            return AreasInfosSuperSerializer


